<?php
//index CLIENT
	session_start();
	//include config	
	include("config.php");
	
	
	//connect database
	$link = mysql_connect(DATABASE_ADDRESS, DATABASE_USER, DATABASE_PASS);
	mysql_select_db(DATABASE_NAME);
	
	//include fonction
	include("fonction.php");
	include("class_download.php");
	
	//gérer le telechargement
	if (isset($_GET['id']) and $_GET['id'] != ""){
		$down = new download($_GET['id']);
		$down->file_generate_header();
	exit();	
	}
	
	//get id
	if (isset($_GET['action']) and $_GET['action'] == "getid"){
		if (isset($_GET['hash']) and $_GET['hash'] != ""){
				$server = check_pin_serv($_GET['hash']);
				if ($server != false){
					if (isset($_GET['user']) and isset($_GET['film'])){
						$id = register_id($_GET['film'],$_GET['user'],$server);
						print "<id>".$id."</id>";
						exit();
					}
					else{
						print "Bad hash";exit();
					}
				}
				else{
					print "Bad hash";exit();
				}
		}
	}
	
	//gestion d'une demande de film d'un des servers de la base
		if (isset($_GET['serv_request']) and $_GET['serv_request'] != ""){
			return_array($_GET['serv_request']);		
		}
	//identifier l'utilisateur
	if (isset($_POST['hidden']) and $_POST['hidden']=="user_login"){
		if (! login_user($_POST['user_login'],$_POST['user_passwd'])){
			$_SESSION['error'] = "Login/password error";
		}
	}
	
	//le reste des fonctions sont autoriser pour un user authentifié
	if ($_SESSION['exist']==1){
		//gestion du cas de suppression d'un dossier
		if (isset($_POST['hidden']) and $_POST['hidden']=='folder_remove'){	
			remove_folder($_POST['folder_box']);
		}
		
		//gestion d'ajout d'un dossier
		if (isset($_POST['hidden']) and $_POST['hidden']=='add_partage'){
			if (isset($_POST['partage_name']) and isset($_POST['partage_chemin']) and $_POST['partage_name']!= "" and $_POST['partage_chemin']!=""){	
				if (add_folder($_POST['partage_name'],$_POST['partage_chemin']) == false){
					
				}
			}
			else{
				$_SESSION['error']="name or directory missing";
			}
		}
		//gestion d'ajout d'un server
		if (isset($_POST['hidden']) and $_POST['hidden']=='add_server'){
			if (isset($_POST['server_name']) and isset($_POST['server_pin']) and $_POST['server_name']!= "" and $_POST['server_pin']!=""){
				add_server($_POST['server_name'],$_POST['server_address'],$_POST['server_pin']);
			}
			else{
				$_SESSION['error']="Name or pin missing";
			}
		}
		//gestion du cas de suppression d'un server
		if (isset($_POST['hidden']) and $_POST['hidden']=='server_remove'){	
			remove_server($_POST['server_box']);
		}
		//disconnect user
		if (isset($_GET['disconnect']) and $_GET['disconnect']!=""){
			disconnect_user();
		}
	}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
	<title><?php echo $html_title ?></title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link rel="stylesheet" href="style.css" type="text/css" />
	
</head>
<body>

	<?php
	//authentification de l'utilisateur
		if($_SESSION['exist']!=1){
	?>
	<div id="total">
	<?php
		//affichage des erreurs
		if (isset($_SESSION['error']) and $_SESSION['error'] != ""){
			echo "<div id=\"id_error\"> ERROR : ".$_SESSION['error']."</div>";
			$_SESSION['error'] = "";
		}
	?>
		<form action="index.php" method="post" >
		<table id="login">
		<caption>Search Client</caption>
		<tr>
		<td>Login</td><td><input type = "text" name="user_login" /></td>
		</tr>
		<tr>
		<td>Password</td><td><input type="password" name="user_passwd" /></td>
		</tr>
		<tr>
		<td colspan="2"><input type="hidden" name="hidden" value="user_login" /><input id="ok" type="submit" value="ok" /></td>
		</tr>
		</table>
		</form>
	</div>
	<?php 		
		}		
		//si utilisateur identifier 
		else{?>
		<div id="total2">
		<div><a href="index.php?disconnect=ok">disconnect</a></div>
		<?php
		//affichage des erreurs
		if (isset($_SESSION['error']) and $_SESSION['error'] != ""){
			echo "<div id=\"id_error\"> ERROR : ".$_SESSION['error']."</div>";
			$_SESSION['error'] = "";
		}
		?>
		<?php 
		//partie html affichage/suppression des partage existants
		$sql_query=mysql_query("Select id,name,chemin from ".DATABASE_TABLE_FOLDER." ORDER BY `id` ASC") or die(mysql_error());
		echo '<div id="div_list_folder">';
			echo '<form action= "index.php" method="post">';
			echo '<table>';
			echo "<caption>List of foler</caption>";
				echo '<tr>';
				echo '<td colspan="2">';
					echo 'Nom du partage';
				echo'</td>';
				echo'<td>';
					echo 'Chemin complet du partage';
				echo'</td>';
			echo '</tr>';
			$i=0;
				while ($query = mysql_fetch_array($sql_query) ){
				$i=1;
				echo '<tr>';
					echo '<td>';
					echo '<input type="checkbox" name="folder_box[]" value="'.$query['id'].'">';
					echo '</td>';
					echo'<td>';
					echo $query['name'];
					echo'</td>';
					echo'<td>';
					echo $query['chemin'];
					echo'</td>';
				echo '</tr>';
				}
			
			echo '<input type="hidden" name="hidden" value="folder_remove">';
			
			
			if ($i==1){
				echo '<tr>';
				echo '<td colspan="3"><input type="submit" value="Supprimer" /></td>';
				echo '</tr>';
			}
			echo '</table>';
			echo '</form>';
		echo '</div><br />';
		
		//partie html affichage/suppression des partage existants
		$sql_query=mysql_query("Select id,name,address,pin from ".DATABASE_TABLE_SERVER." ORDER BY `id` ASC") or die(mysql_error());
		echo '<div id="div_list_server">';
			echo '<form action= "index.php" method="post">';
			echo '<table>';
			echo "<caption>List of server</caption>";
			echo '<tr>';
				echo '<td colspan="2">';
					echo 'Nom du partage';
				echo'</td>';
				echo'<td>';
					echo 'Adresse du serveur';
				echo'</td>';
				echo'<td>';
					echo 'Code pin du serveur';
				echo'</td>';
			echo '</tr>';
			$i=0;
				while ($query = mysql_fetch_array($sql_query) ){
				$i=1;
				echo '<tr>';
					echo '<td>';
					echo '<input type="checkbox" name="server_box[]" value="'.$query['id'].'">';
					echo '</td>';
					echo'<td>';
					echo $query['name'];
					echo'</td>';
					echo'<td>';
					echo $query['address'];
					echo'</td>';
					echo'<td>';
					echo '<input type ="button" value="See" onclick="alert(\'Pin : '.$query['pin'].'\')" />';
					echo'</td>';
				echo '</tr>';
				}
			
			echo '<input type="hidden" name="hidden" value="server_remove">';
			if ($i==1){
				echo '<tr>';
				echo '<td colspan="3"><input type="submit" value="Supprimer" /></td>';
				echo '</tr>';
			}
			echo '</table>';
			echo '</form>';
		echo '</div><br />';
		
	?>
	<div id="affichage_dossier">
	<form action="index.php" method="post">
	<table>
		<caption>Ajout d'un partage</caption>
		<tr>
		<td>Nom*</td><td><input type="text" name="partage_name" /></td>
		</tr>
		<tr>
		<td>chemin*</td><td><input type="text" name="partage_chemin" /></td>
		</tr>
		<tr>
		<td><input type="hidden" name="hidden" value="add_partage" /><input type="submit" value="OK" /></td>
		</tr>
	</table>
	</form>
	</div>
	
	<div id="affichage_server">
	<form action="index.php" method="post">
	<table>	
		<caption>Ajouter un serveur</caption>
		<tr>
		<td>Nom*</td><td><input type="text" name="server_name" /></td>
		</tr>
		<tr>
		<td>code*</td><td><input type="text" name="server_pin" /></td>
		</tr>
		<tr>
		<td>adresse</td><td><input type="text" name="server_address" /></td>
		</tr>
		<tr>
		<td><input type="hidden" name="hidden" value="add_server" /><input type="submit" value="OK"></td>
		</tr>
	</table>
	</form>
	</div>	
</div>
<div id="div_log">
		<table>
		<caption>Log <a href="" onclick="window.location='index.php';">refresh</a></caption>
		<tr>
		<td>Action</td><td>User</td><td>date</td><td>server</td>
		</tr>
		<?php 
		$sql_query = mysql_query("Select action,server,user,date from ".DATABASE_TABLE_LOG." ORDER BY `date` DESC LIMIT 0 , 15");
		while ($query = mysql_fetch_array($sql_query) or die(mysql_error()) ){
		?>
		<tr  bgcolor="#ffffff" onmouseout="this.bgColor='#FFFFFF';" onmouseover="this.bgColor='#DADADA';" >
		<td class="log_action"><?php print $query['action']; ?></td><td class = "log_user"><?php print $query['user']; ?></td><td class ="log_date"><?php print date( "d/m/y à G\h i\m",$query['date']) ; ?></td><td class="log_server"><?php print $query['server']; ?></td>
		</tr>
		<?php
		}
		?>
		</table>
</div>

	<?php
		}
	//fin de authentified user
	?>

</body>
</html>
<?php mysql_close($link)?>